package com.kaifamiao.wendao.dao;

import com.kaifamiao.wendao.entity.Thumbs;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ThumbsDao extends BaseDao<Thumbs,Long>{
    private final static String INSERT_ONE="INSERT INTO t_thiumbs (id,customer_id,explain_id,operation) VALUES(?,?,?,?)";
    private final static String SELECT_ONE="SELECT id,customer_id,explain_id,operation FROM t_thiumbs WHERE id=?";
    private final static String SELECT_ALL="SELECT id,customer_id,explain_id,operation FROM t_thiumbs";
    private final static String UPDATE_ONE="UPDATE t_thiumbs SET customer_id=?,explain_id=?,operation=? WHERE customer_id=? AND id=? ";
    private final static String SELECT_CIDEID="SELECT id,customer_id,explain_id,operation FROM t_thiumbs WHERE explain_id=? AND customer_id=?";
    public boolean save(Thumbs t){
        Object[] params={t.getId(),t.getCustomer_id(),t.getExplain_id(),t.getOperation()};
        try {
            return runner.update(INSERT_ONE,params)==1;
        } catch (SQLException cause) {
            throw  new RuntimeException("插入失败",cause);
        }
    }

    @Override
    public boolean remove(Long id) {
        return false;
    }

    @Override
    public boolean update(Thumbs thumbs) {
        Object[] params={thumbs.getCustomer_id(),thumbs.getExplain_id(),thumbs.getOperation(),thumbs.getCustomer_id(),thumbs.getId()};
        try {
            runner.update(UPDATE_ONE,params);
        } catch (SQLException e) {
            throw new RuntimeException("更新失败");
        }
        return false;
    }

    private Thumbs wrap(ResultSet rs) throws SQLException{
        Thumbs t=new Thumbs();
        t.setId(rs.getLong("id"));
        t.setCustomer_id(rs.getLong("customer_id"));
        t.setExplain_id(rs.getLong("explain_id"));
        t.setOperation(rs.getLong("operation"));
        return t;
    }

    @Override
    public List<Thumbs> findAll() {
        ResultSetHandler<List<Thumbs>> thumbs=rs->{
            List<Thumbs> thumbs1=new ArrayList<>();
            while (rs.next()){
                Thumbs t=this.wrap(rs);
                thumbs1.add(t);
            }
            return thumbs1;
        };
        try {
            return runner.query(SELECT_ALL,thumbs);
        } catch (SQLException e) {
            throw new RuntimeException("查询失败");
        }
    }

    @Override
    public Thumbs find(Long id) {
        ResultSetHandler<Thumbs> thumbs=rs->{
            if(rs.next()){
                Thumbs t=this.wrap(rs);
                return t;
            }
            return null;
        };
        try {
            return runner.query(SELECT_ONE,thumbs,id);
        } catch (SQLException e) {
            throw new RuntimeException("查询失败");
        }
    }

    public Thumbs findCidEid(Long eid,Long cid) {
        ResultSetHandler<Thumbs> thumbs=rs->{
            if(rs.next()){
                Thumbs t=this.wrap(rs);
                return t;
            }
            return null;
        };

        try {
            return runner.query(SELECT_CIDEID,thumbs,eid,cid);
        } catch (SQLException e) {
            throw new RuntimeException("查询失败");
        }

    }
}
